let port = 3005;

const path = require('path');

const Koa = require("koa");
const render = require('koa-ejs');
const serve = require('koa-static');
const bodyParser = require('koa-bodyparser');
const session = require('koa-session');
const compose = require('koa-compose');

// 加载自定义中间件
const logger = require('./middlewares/logger');
const controller = require('./middlewares/controller');

// 加载数据库
const { connect, initSchemas } = require('./database/init');
connect();  // 连接数据库
initSchemas();  // 初始化数据模版


const app = new Koa();

// 使用 session
app.keys = ['some secret hurr'];
const CONFIG = {
    renew: true
};
// app.use(session(app));


// 加载模版引擎
render(app, {
    root: path.join(__dirname, 'views'),
    // layout: 'template',
    layout: false,
    viewExt: 'ejs',
    cache: false,
});

// 设置静态资源目录
// app.use(serve("./public"));
// parse request body:
// app.use(bodyParser());


// add controllers:
// app.use(controller('./controllers'));

const middlewares = compose([logger, session(app), serve("./public"), bodyParser(), controller('../controllers')]);
app.use(middlewares);

app.listen(port);
console.log("Your server is running at 127.0.0.1:" + port);